package joc.internal.transformer.helper;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.NotFoundException;

public class RegexpImplicitContract extends ImplicitContract {

	private String affectedMethodRegexp;

	public RegexpImplicitContract(String configValue, Class<? extends Annotation> contractAnnotation,
			String affectedMethodRegexp) {
		super(configValue, contractAnnotation);
		this.affectedMethodRegexp = affectedMethodRegexp;
	}

	@Override
	public List<CtMethod> getAffectedMethods(ClassPool pool, CtClass targetClass) throws NotFoundException {
		List<CtMethod> affectedMethods = new ArrayList<CtMethod>();
		System.out.println("looking for regexp " + affectedMethodRegexp);
		for (CtMethod targetMethod : targetClass.getDeclaredMethods()) {
			System.out.println("method " + targetMethod.getName());
			if (Pattern.matches(affectedMethodRegexp, targetMethod.getName())) {
				System.out.println("match");
				affectedMethods.add(targetMethod);
			}
		}
		return affectedMethods;
	}

}
